<template>
	<view class="index">
		<view class="nabbar" :style="{'height': statusBarHeight + barHeight + 'px'}">
			<!-- //手机状态栏 -->
			<view class="status-bar" :style="{'height': statusBarHeight + 'px'}"></view>
			<!-- //导航栏 -->
			<view class="nabbar-box" :style="{ 'top': statusBarHeight+ 'px', 'heigth': barHeight + 'px' }">
				<view class="nabbar-cont">
					<view>达人系统</view>
				</view>
			</view>
		</view>
		<image :src="banner" class="banner"></image>
		<view class="search">
			<uni-search-bar @input="search" placeholder="搜索任务" bgColor="#fff" />
		</view>
		<view class="flex align between top-nav">
			<navigator hover-class="none" url="/pages/resign/resign" class="nav-item flex align">
				<image src="../../static/images/index/nav1.png" class="nav-bg"></image>
				<image src="../../static/images/index/nav1-i.png" class="nav-icon"></image>
				<view class="nav-info">
					<view>我是商家</view>
					<view>入驻发布任务</view>
				</view>
			</navigator>
			<navigator hover-class="none" url="/pages/resign/userResign" class="nav-item flex align">
				<image src="../../static/images/index/nav2.png" class="nav-bg"></image>
				<image src="../../static/images/index/nav2-i.png" class="nav-icon"></image>
				<view class="nav-info">
					<view style="color: #FF9200;">我是达人</view>
					<view>点击入驻赚钱</view>
				</view>
			</navigator>
		</view>
		<view class="list-main">
			<view v-if="description!=''">
				<uni-notice-bar show-icon scrollable single :text="description"></uni-notice-bar>
			</view>
			<view class="flex align tabs">
				<view :class="act==1?'act':''" @click="toAct(1)">探店任务</view>
				<view :class="act==2?'act':''" @click="toAct(2)">短视频任务</view>
			</view>
			<view class="cards">
				<!-- <view class="card-select flex align between">
					<view class="flex align">
						<view>发布时间</view>
						<uni-icons type="down" size="20"></uni-icons>
					</view>
					<view class="flex align">
						<view>发布地点</view>
						<uni-icons type="down" size="20"></uni-icons>
					</view>
					<view class="flex align">
						<view>发布平台</view>
						<uni-icons type="down" size="20"></uni-icons>
					</view>
				</view> -->
				<navigator :url="'/pages/task/info?id='+item.id+'&type='+act" hover-class="none" class="list" v-for="(item,index) in list"
					:key="index">
					<view class="list-info1 flex between">
						<image :src="item.store_text[0].logo"></image>
						<view class="info1-info" style="width:80%;">
							<view>{{item.title}}</view>
							<view v-if="act==1">探店日期：{{item.other.visit_store.dates[0]}}</view>
							<view v-if="act==1">接待人数：{{item.other.visit_store.day_person_limit}}</view>
							<view>截止日期：{{item.deadline_time}}</view>
							<view>粉丝数量：{{item.fans}}人</view>
						</view>
						<!-- <view class="price" style="width: 23%;" v-if="item.level_text!=0">V2及以上</view> -->
						<!-- <view class="price" v-if="item.level_text==0" style="width: 23%;"></view> -->
					</view>
					<view class="list-info2 flex align between">
						<view class="tags flex align">
							<text>{{item.cost_type_text}}</text>
							<text>{{item.app_text}}任务</text>
							<text v-if="item.region_limit!=''">地区限制：{{item.region_limit}}</text>
							<!-- <text v-for="(item2,index2) in item.signing_condition_text" :key="index2">{{item2}}</text> -->
						</view>
						<view class="nums">
							招募{{item.num}}人 /剩余{{item.remain_num}}人
						</view>
					</view>
				</navigator>
				<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
				<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
			</view>
		</view>
		<!-- <image @click="toUrl()" src="../../static/images/index/icon.png" class="send-btn"></image> -->
	</view>
</template>
<script>
	import {
		minilogin,
		checkToken,
		positionDetail,
		taskLists
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				name: '',
				act: 1,
				statusBarHeight: 20,
				barHeight: 44,
				barWidth: null,
				banner: '',
				description: '',
				totalPage: 1,
				page: 0,
				offset: 10,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
			}
		},
		onLoad() {
			let that = this;
			//通过uni自带api获取手机系统信息
			uni.getSystemInfo({
				success: function(res) {
					// console.log(res)
					that.statusBarHeight = res.statusBarHeight; //手机状态栏高度
					let isiOS = res.system.indexOf('iOS') > -1; //是否为iOS系统
					that.barHeight = !isiOS ? 48 : 44; //导航栏高度，iOS：48，Android：44
					that.barWidth = res.windowWidth - 87; //nabbar可操作宽度 = 屏幕宽度 - 小程序右上角的胶囊宽度（87）
				}
			})
			this.getBanner()
			this.getlist()
		},
		onShow() {
			let that = this;
			let token = uni.getStorageSync('token')
			if (token) {
				that.checkToken(token)
			} else {
				console.log(222)
				that.wxLogin()
			}
		},
		onReady() {

		},
		onShareAppMessage(res) {

		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					act,
					name
					// channel_id,
				} = that;
				let obj = {
					size: offset,
					page: page + 1,
					task_type: act == 1 ? 2 : 1,
					title:name
					// channel_id: channel_id
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await taskLists(obj);
					if (result.code == 1) {
						that.page = page + 1;
						if (result.data.total == 0) {
							console.log((888))
							that.empty = true;
							that.status = 'nomore';
							return;
						}
						that.empty = false;
						setTimeout(() => {
							that.status = 'loadmore';
						}, 500);
						that.list = list.concat(result.data.list);
						that.totalPage = Math.ceil(result.data.total / offset);
					}
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
			async getBanner() {
				const result = await positionDetail({
					position_id: 2
				})
				const result2 = await positionDetail({
					position_id: 1
				})
				this.banner = result.data.path
				this.description = result2.data.description
			},
			async checkToken(token) {
				console.log(token)
				const result = await checkToken({
					token: token
				})
				console.log(result)
				if (result.code == 1) {} else {
					this.wxLogin()
				}
			},
			wxLogin() {
				let that = this
				uni.getUserInfo({
					desc: '登录的数据',
					success(ures) {
						console.log(ures, '返回的用户信息');
						uni.login({
							provider: 'wechat',
							success: function(res) {
								console.log('微信登录成功', res)
								let obj = {
									code: res.code,
									encryptedData: ures.encryptedData,
									iv: ures.iv
								}
								that.obj = obj
								that.wxAppLogin()
							},
							fail: function(err) {
								console.log('微信登录失败', err)
							}
						})
					}
				})
			},
			async wxAppLogin() {
				let obj = this.obj
				const result = await minilogin(obj)
				if (result.code == 1) {
					uni.setStorageSync('token', result.data.token);
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					});
				}
			},
			search(e) {
				this.name = e
				this.page = 0
				this.list = []
				this.getlist();
			},
			toAct(e) {
				this.act = e
				this.page = 0
				this.list = []
				this.getlist();
			},
			toUrl() {
				uni.navigateTo({
					url: '/pages/task/index'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.banner {
		width: 100%;
		height: 525rpx;

	}

	.search {
		position: absolute;
		top: 138rpx;
		width: 100%;
	}

	.top-nav {
		margin: 0 20rpx;

		.nav-item {
			position: relative;
			width: 336rpx;
			height: 182rpx;
			justify-content: center;
		}

		.nav-bg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
		}

		.nav-icon {
			position: relative;
			width: 74rpx;
			height: 80rpx;
		}

		.nav-info {
			position: relative;
			margin-left: 30rpx;

			view:nth-child(1) {
				font-size: 24rpx;
				font-weight: bold;
				color: #FF6543;
				margin-bottom: 5rpx;
			}

			view:nth-child(2) {
				font-size: 16rpx;
			}
		}
	}

	.list-main {
		margin: 20rpx;

		.tabs {
			font-size: 32rpx;
			font-weight: bold;

			view {
				margin-right: 18rpx;
				position: relative;
				padding: 10rpx 0;
			}

			.act::after {
				content: "";
				position: absolute;
				bottom: 0;
				width: 50%;
				left: 25%;
				height: 9rpx;
				background: #FF6543;
				border-radius: 5rpx;
			}
		}

		.cards {
			background: white;
			border-radius: 10rpx;
			box-shadow: 1rpx 1rpx 1rpx 4rpx #e8e8e8;
			margin-top: 20rpx;

			.card-select {
				font-weight: bold;
				font-size: 24rpx;
				padding: 12rpx 53rpx;
				border-bottom: solid 1rpx gainsboro;
			}

			.list {
				margin: 16rpx;
				border-bottom: solid 1rpx gainsboro;
				padding-bottom: 23rpx;

				.list-info1 {
					padding: 30rpx;

					image {
						width: 138rpx;
						height: 138rpx;
						border-radius: 12rpx;
					}

					.info1-info {
						margin-left: 24rpx;
						color: #AAAAAA;
						font-size: 18rpx;

						view:nth-child(1) {
							font-size: 32rpx;
							font-weight: bold;
							margin-bottom: 20rpx;
							color: #2D2D2D;
						}
					}

					.price {
						color: #FF6543;
						font-size: 30rpx;
						font-weight: bold;
					}
				}

				.list-info2 {
					.tags {
						color: #FF6543;
						font-size: 18rpx;

						text {
							border: solid 1rpx;
							padding: 2rpx 14rpx;
							border-radius: 6rpx;
							margin-right: 12rpx;
						}
					}

					.nums {
						color: #FF6543;
						font-size: 24rpx;
						font-weight: bold;
					}
				}
			}
		}
	}

	.send-btn {
		width: 124rpx;
		height: 124rpx;
		position: fixed;
		bottom: 120rpx;
		right: 16rpx;
	}
</style>